package com.gitee.attendance.util.baidu;

import java.util.HashMap;
import java.util.Map;

import com.baidu.aip.face.AipFace;
import com.gitee.attendance.util.http.HttpUtil;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class CurrencyUtil{


    @Value("${baidu.app-id}")
    private String APP_ID;

    @Value("${baidu.api-key}")
    private String API_KEY;

    @Value("${baidu.secret-key}")
    private String SECRET_KEY;


    private static AipFace aipFace;
    private static String accessToken;

    /**
     * @param ERROR_CODE the error_code
     */
    public static String ERROR_CODE = "error_code";
    /**
     * @param ERROR_MSG the error_msg
     */
    public static String ERROR_MSG = "error_msg";
    /**
     * @param RESULT the result
     */
    public static String RESULT = "result";
    /**
     * @param FACE_LIVENESS the face_liveness
     */
    public static String FACE_LIVENESS = "face_liveness";
    /**
     * @param THRESHOLDS the thresholds
     */
    public static String THRESHOLDS = "thresholds";
    /**
     * @param FRR_1E the frr_1e-2
     */
    public static String FRR_1E = "frr_1e-2";
    /**
     * @param String the score
     */
    public static String SCORE = "score";

    /**
     * @param BASE64 the BASE64
     */
    public static String BASE64 = "BASE64";

    /**
     * @param URL the URL
     */
    public static String URL = "URL";


    private Logger logger = LoggerFactory.getLogger(FaceUtil.class);

    public AipFace getAipFace() {
        if (aipFace == null) {
            aipFace = new AipFace(APP_ID, API_KEY, SECRET_KEY);
            logger.info("-------------->百度接口启动");
        }
        return aipFace;
    }

    public String getAuth() {
        if(accessToken==null){
            // 获取token地址
            String path = "https://aip.baidubce.com/oauth/2.0/token";

            Map<String, String> date = new HashMap<String,String>();
            date.put("grant_type", "client_credentials");
            date.put("client_id", API_KEY);
            date.put("client_secret", SECRET_KEY);

            String result = HttpUtil.startGet(path, date);
            /**
             * 返回结果示例
             */

            JSONObject jsonObject = new JSONObject(result);
            accessToken = jsonObject.getString("access_token");
            logger.info("-------------->获取百度接口access_token:"+accessToken);
        }
        return accessToken;

    }
}